﻿Imports System.Data.OleDb
Imports System.DBNull
Imports System.Data.SqlClient
Public Class Dausach
    Public db As New Class1
    Dim gt As Boolean
    Public sql As String
    Public mycom As OleDbCommand
    Dim con As OleDbConnection
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        db.connectDB()
        showDataGrid()
        khoa()
        sql = "select * from LoaiSach"
        db.strSQL(sql)
        While db.myda.Read()
            cbmaloaisach.Items.Add(db.myda.GetString(0))
        End While
        sql = "select * from TacGia"
        db.strSQL(sql)
        While db.myda.Read()
            cbmatacgia.Items.Add(db.myda.GetString(0))
        End While
        sql = "select * from NhaXB"
        db.strSQL(sql)
        While db.myda.Read()
            cbnxb.Items.Add(db.myda.GetString(0))
        End While
        sql = "select * from NgonNgu"
        db.strSQL(sql)
        While db.myda.Read()
            cbngonngu.Items.Add(db.myda.GetString(0))
        End While
    End Sub
    Sub khoa()
        Me.madausach.Enabled = False
        Me.tendausach.Enabled = False
        Me.cbmaloaisach.Enabled = False
        Me.cbmatacgia.Enabled = False
        Me.cbngonngu.Enabled = False
        Me.cbnxb.Enabled = False
        Me.giatien.Enabled = False
        Me.sotrang.Enabled = False
        Me.namxuatban.Enabled = False
        Me.dangluutru.Enabled = False
    End Sub
    Sub mo()
        Me.madausach.Enabled = True
        Me.tendausach.Enabled = True
        Me.cbmaloaisach.Enabled = True
        Me.cbmatacgia.Enabled = True
        Me.cbngonngu.Enabled = True
        Me.cbnxb.Enabled = True
        Me.giatien.Enabled = True
        Me.sotrang.Enabled = True
        Me.namxuatban.Enabled = True
        Me.dangluutru.Enabled = True

    End Sub
    Sub xoa()
        Me.madausach.Text = ""
        Me.tendausach.Text = ""
        Me.cbmaloaisach.Text = "chọn..."
        Me.cbngonngu.Text = "chọn..."
        Me.cbnxb.Text = "chọn..."
        Me.cbmatacgia.Text = "chọn..."
        Me.giatien.Text = ""
        Me.sotrang.Text = ""
        Me.namxuatban.Text = ""
        Me.dangluutru.Text = ""
    End Sub
    Function samekey()
        Dim lenh As String = " Select MaDS from DauSach where MaDS='" & Me.madausach.Text & "'"
        Dim mycom As New OleDbCommand(lenh, db.myconn)
        Dim mydr As OleDbDataReader = mycom.ExecuteReader()
        Return mydr.HasRows()
    End Function
    Sub showDataGrid()
        sql = "Select  MaDS as 'Mã Đầu Sách' , TenDS as 'Tên Đầu Sách', MaLS as 'Mã Loại Sách', MaTG as 'Mã Tác Giả', MaNXB  as 'Mã Nhà Xuất Bản', MaNN  as 'Mã Ngôn Ngữ', GiaTien  as 'Giá Tiền', SoTrang as 'Số Trang', NamXB  as 'Năm Xuất Bản', DangLuuTru  as 'Dạng Lưu Trữ' From DauSach"
        db.iniDataset(sql)
        Me.viewdausach.DataSource = db.mydataset.Tables(0)
    End Sub
    Private Sub them()
        showDataGrid()
        Dim sql As String = "insert into DauSach values(N'" & Me.madausach.Text.ToUpper & "',N'" & Me.tendausach.Text & "',N'" & Me.cbmaloaisach.Text & "',N'" & Me.cbmatacgia.Text & "',N'" & Me.cbnxb.Text & "',N'" & Me.cbngonngu.Text & "',N'" & Me.giatien.Text & "',N'" & Me.sotrang.Text & "',N'" & Me.namxuatban.Text & "',N'" & Me.dangluutru.Text & "');"
        If samekey() = True Then
            MsgBox("Mã Đầu Sách bạn nhập hiện đã có trong CSDL. Bạn vui lòng nhập lại!")
            Me.madausach.Focus()
        Else
            db.doSQL(sql)
            MsgBox("Chúc mừng! Bạn đã thêm thành công!")
        End If
        showDataGrid()
    End Sub
    Sub sua()
        sql = "UPDATE DauSach SET  TenDS=N'" + Me.tendausach.Text + "', MaLS=N'" + Me.cbmaloaisach.Text + "', MaTG=N'" + Me.cbmatacgia.Text + "', MaNXB=N'" + Me.cbnxb.Text + "', MaNN=N'" + Me.cbngonngu.Text + "', GiaTien=N'" + Me.giatien.Text + "', SoTrang=N'" + Me.sotrang.Text + "', NamXB=N'" + Me.namxuatban.Text + "', DangLuuTru=N'" + Me.dangluutru.Text + "' where MaDS=N'" + Me.madausach.Text.ToUpper + "'"
        db.doSQL(sql)
        Me.showDataGrid()
    End Sub

    Private Sub btthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btthem.Click
        db.connectDB()
        xoa()
        btluu.Enabled = True
        bthuy.Enabled = True
        btthem.Enabled = False
        btsua.Enabled = False
        mo()
        gt = True
        Me.viewdausach.Enabled = False
    End Sub

    Private Sub btsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btsua.Click
        db.connectDB()

        If Me.madausach.Text = "" Then
            MsgBox("chua chua truong de sua")
        Else
            Me.viewdausach.Enabled = False
            btluu.Enabled = True
            bthuy.Enabled = True
            btthem.Enabled = False
            btsua.Enabled = False
            mo()
            Me.madausach.Enabled = False
            gt = False
        End If
    End Sub

    Private Sub btxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btxoa.Click
        db.connectDB()
        sql = "delete From DauSach where MaDS='" & Me.madausach.Text & "'"
        If Me.madausach.Text <> "" Then
            If MsgBox("Bạn có thật sự muốn xóa các thông tin về " & Me.madausach.Text, MsgBoxStyle.OkCancel, "Xác nhận") = MsgBoxResult.Ok Then
                db.doSQL(sql)
            End If
        Else : MsgBox("Bạn chưa chọn thông tin cần xóa", MsgBoxStyle.Information, "Thông báo")
        End If
        Me.showDataGrid()
    End Sub

    Private Sub btluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btluu.Click
        If gt = True Then
            them()
        Else
            sua()
        End If
    End Sub

    Private Sub bthuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bthuy.Click
        btluu.Enabled = False
        bthuy.Enabled = False
        btthem.Enabled = True
        btsua.Enabled = True
        khoa()
        xoa()
        Me.viewdausach.Enabled = True
    End Sub

    Private Sub viewdausach_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles viewdausach.CellContentClick
        Try
            Me.madausach.Text = viewdausach.CurrentRow.Cells(0).Value()
            Me.tendausach.Text = viewdausach.CurrentRow.Cells(1).Value()
            Me.cbmaloaisach.Text = viewdausach.CurrentRow.Cells(2).Value()
            Me.cbmatacgia.Text = viewdausach.CurrentRow.Cells(3).Value()
            Me.cbnxb.Text = viewdausach.CurrentRow.Cells(4).Value()
            Me.cbngonngu.Text = viewdausach.CurrentRow.Cells(5).Value()
            Me.giatien.Text = viewdausach.CurrentRow.Cells(6).Value()
            Me.sotrang.Text = viewdausach.CurrentRow.Cells(7).Value()
            Me.namxuatban.Text = viewdausach.CurrentRow.Cells(8).Value()
            Me.dangluutru.Text = viewdausach.CurrentRow.Cells(9).Value()
        Catch ex As Exception
        End Try
    End Sub
End Class